<template>
  <!-- 主体区域 -->
  <section id="app">
    <TodoHeader @add="handleAdd"></TodoHeader>
    <TodoMain :list="list" @del="handleDel"></TodoMain>
    <TodoFooter :list="list" @clear="handleClear"></TodoFooter>
  </section>
</template>

<script>
import TodoFooter from './components/TodoFooter.vue';
import TodoMain from './components/TodoMain.vue';
import TodoHeader from './components/TodoHeader.vue';

export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem('list')) || [
        { id: 1, name: '打篮球' },
        { id: 2, name: '看电影' },
        { id: 3, name: '逛街' },
      ],
    }
  },
  components: {
    TodoFooter, TodoMain, TodoHeader
  },
  methods: {
    handleAdd(value) {
      this.list.unshift({ id: +new Date(), name: value })
    },
    handleDel(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    handleClear() {
      this.list = []
    }
  },
  watch: {
    list: {
      deep: true,
      handler(newValue) {
        localStorage.setItem('list', JSON.stringify(newValue))
      }
    }
  }
}
</script>

<style></style>
